<?php
class ModelCommunityForum extends Model {
	public function addForum($data) {
        $this->db->query("INSERT INTO " . DB_PREFIX . "community_forum SET sort_order_id='" . (int)$data['sort_order'] . "', forum_cat_id='" . (int)$data['category_id'] . "', parent_id='" . (int)$data['parent_id'] . "', topic_category_enabled='" . (int)$data['topic_category_enabled'] . "', topic_category_displayed='" . (int)$data['topic_category_displayed'] . "', forum_type='" . (int)$data['type'] . "', forum_enabled='" . (int)$data['status'] . "'");

        $forum_id=$this->db->getLastId();

        foreach($data['language_ids'] as $language_id){
            $this->db->query("INSERT INTO " . DB_PREFIX . "community_forum_name_des_rule SET language_id='" . (int)$language_id . "', forum_id='" . (int)$forum_id . "', forum_name='" . $this->db->escape($data['name'][$language_id]) . "', forum_desc='" . $this->db->escape($data['description'][$language_id]) . "', forum_rules='" . $this->db->escape($data['rule'][$language_id]) . "'");
        }

        if(isset($data['tag'])){
            foreach($data['tag'] as $tag){
                $this->db->query("INSERT INTO " . DB_PREFIX . "community_topic_category SET forum_id='" . (int)$forum_id . "', sort_order_id='" . (int)$tag['sort_order_id'] . "'");

                $topic_category_id=$this->db->getLastId();

                foreach($tag['topic_category_title'] as $key => $topic_category_title){
                    $this->db->query("INSERT INTO " . DB_PREFIX . "community_topic_category_title SET topic_category_id='" . (int)$topic_category_id . "', forum_id='" . (int)$forum_id . "', language_id='" . (int)$key . "', topic_category_title='" . $this->db->escape($topic_category_title) . "'");
                }
            }
        }

        if($data['type']){
            //add user rank permission
            foreach($this->getAllUserRankIds() as $result){
                $this->db->query("INSERT INTO " . DB_PREFIX . "community_permission SET forum_id='" . (int)$forum_id . "', role_id='5', rank_id='" . (int)$result['rank_id'] . "'");
            }

            //add user group permission
            foreach($this->getAllUserDefinedUserGroups() as $result){
                $this->db->query("INSERT INTO " . DB_PREFIX . "community_permission SET forum_id='" . (int)$forum_id . "', role_id='" . (int)$result['role_id'] . "', rank_id='0'");
            }

            //add other user group permission
            $this->db->query("INSERT INTO " . DB_PREFIX . "community_permission SET forum_id='" . (int)$forum_id . "', role_id='1', rank_id='0'");
            $this->db->query("INSERT INTO " . DB_PREFIX . "community_permission SET forum_id='" . (int)$forum_id . "', role_id='2', rank_id='0'");
            $this->db->query("INSERT INTO " . DB_PREFIX . "community_permission SET forum_id='" . (int)$forum_id . "', role_id='3', rank_id='0'");
            $this->db->query("INSERT INTO " . DB_PREFIX . "community_permission SET forum_id='" . (int)$forum_id . "', role_id='4', rank_id='0'");
            $this->db->query("INSERT INTO " . DB_PREFIX . "community_permission SET forum_id='" . (int)$forum_id . "', role_id='6', rank_id='0'");
            $this->db->query("INSERT INTO " . DB_PREFIX . "community_permission SET forum_id='" . (int)$forum_id . "', role_id='7', rank_id='0'");
        }
	}
	
	public function editForum($forum_id, $data) {
        $this->db->query("UPDATE " . DB_PREFIX . "community_forum SET sort_order_id='" . (int)$data['sort_order'] . "', forum_cat_id='" . (int)$data['category_id'] . "', parent_id='" . (int)$data['parent_id'] . "', topic_category_enabled='" . (int)$data['topic_category_enabled'] . "', topic_category_displayed='" . (int)$data['topic_category_displayed'] . "', forum_type='" . (int)$data['type'] . "', forum_enabled='" . (int)$data['status'] . "' WHERE forum_id='" . (int)$forum_id . "'");

        $this->db->query("DELETE FROM " . DB_PREFIX . "community_forum_name_des_rule WHERE forum_id='" . (int)$forum_id . "'");

        foreach($data['language_ids'] as $language_id){
            $this->db->query("INSERT INTO " . DB_PREFIX . "community_forum_name_des_rule SET language_id='" . (int)$language_id . "', forum_id='" . (int)$forum_id . "', forum_name='" . $this->db->escape($data['name'][$language_id]) . "', forum_desc='" . $this->db->escape($data['description'][$language_id]) . "', forum_rules='" . $this->db->escape($data['rule'][$language_id]) . "'");
        }

        if(isset($data['tag'])){
            foreach($data['tag'] as $tag){
                $query=$this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "community_topic_category WHERE topic_category_id='" . (int)$tag['topic_category_id'] . "'");

                if(!$query->row['total']){
                    $this->db->query("INSERT INTO " . DB_PREFIX . "community_topic_category SET forum_id='" . (int)$forum_id . "', sort_order_id='" . (int)$tag['sort_order_id'] . "'");

                    $topic_category_id=$this->db->getLastId();

                    foreach($tag['topic_category_title'] as $key => $topic_category_title){
                        $this->db->query("INSERT INTO " . DB_PREFIX . "community_topic_category_title SET topic_category_id='" . (int)$topic_category_id . "', forum_id='" . (int)$forum_id . "', language_id='" . (int)$key . "', topic_category_title='" . $this->db->escape($topic_category_title) . "'");
                    }
                }else{
                    $this->db->query("UPDATE " . DB_PREFIX . "community_topic_category SET sort_order_id='" . (int)$tag['sort_order_id'] . "' WHERE topic_category_id='" . (int)$tag['topic_category_id'] . "'");

                    foreach($tag['topic_category_title'] as $key => $topic_category_title){
                        $this->db->query("UPDATE " . DB_PREFIX . "community_topic_category_title SET topic_category_title='" . $this->db->escape($topic_category_title) . "' WHERE topic_category_id='" . (int)$tag['topic_category_id'] . "' AND language_id='" . (int)$key . "'");
                    }
                }
            }
        }

        //update administrative permission

        if(isset($data['administrative_permission']) && $data['administrative_permission']){
            foreach($data['administrative_permission'] as $administrative_permission){
                $this->db->query("UPDATE " . DB_PREFIX . "community_permission SET topic_announcement='" . (int)(isset($administrative_permission['topic_announcement']) ? $administrative_permission['topic_announcement'] : '0') . "', topic_global_announcement='" . (int)(isset($administrative_permission['topic_global_announcement']) ? $administrative_permission['topic_global_announcement'] : '0') . "', topic_featured='" . (int)(isset($administrative_permission['topic_featured']) ? $administrative_permission['topic_featured'] : '0') . "', topic_sticky='" . (int)(isset($administrative_permission['topic_sticky']) ? $administrative_permission['topic_sticky'] : '0') . "', topic_move='" . (int)(isset($administrative_permission['topic_move']) ? $administrative_permission['topic_move'] : '0') . "', topic_lock='" . (int)(isset($administrative_permission['topic_lock']) ? $administrative_permission['topic_lock'] : '0') . "', topic_solve='" . (int)(isset($administrative_permission['topic_solve']) ? $administrative_permission['topic_solve'] : '0') . "', topic_unsolve='" . (int)(isset($administrative_permission['topic_unsolve']) ? $administrative_permission['topic_unsolve'] : '0') . "', edit_lock='" . (int)(isset($administrative_permission['edit_lock']) ? $administrative_permission['edit_lock'] : '0') . "', post_approve='" . (int)(isset($administrative_permission['post_approve']) ? $administrative_permission['post_approve'] : '0') . "', post_recycle_bin_clear='" . (int)(isset($administrative_permission['post_recycle_bin_clear']) ? $administrative_permission['post_recycle_bin_clear'] : '0') . "', post_members_edit='" . (int)(isset($administrative_permission['post_members_edit']) ? $administrative_permission['post_members_edit'] : '0') . "', post_members_delete='" . (int)(isset($administrative_permission['post_members_delete']) ? $administrative_permission['post_members_delete'] : '0') . "' WHERE forum_id='" . (int)$forum_id . "' AND role_id='" . (int)$administrative_permission['role_id'] . "' AND rank_id='" . (int)$administrative_permission['rank_id'] . "'");
            }
        }

        //update administrative group common permission
        if(isset($data['admin_common_permission']) && $data['admin_common_permission']){
            foreach($data['admin_common_permission'] as $admin_common_permission){
                $this->db->query("UPDATE " . DB_PREFIX . "community_permission SET view_forum='" . (int)(isset($admin_common_permission['view_forum']) ? $admin_common_permission['view_forum'] : '0') . "', topic_view='" . (int)(isset($admin_common_permission['topic_view']) ? $admin_common_permission['topic_view'] : '0') . "', topic_publish='" . (int)(isset($admin_common_permission['topic_publish']) ? $admin_common_permission['topic_publish'] : '0') . "', topic_reply='" . (int)(isset($admin_common_permission['topic_reply']) ? $admin_common_permission['topic_reply'] : '0') . "', post_delete='" . (int)(isset($admin_common_permission['post_delete']) ? $admin_common_permission['post_delete'] : '0') . "', post_edit='" . (int)(isset($admin_common_permission['post_edit']) ? $admin_common_permission['post_edit'] : '0') . "', poll_publish='" . (int)(isset($admin_common_permission['poll_publish']) ? $admin_common_permission['poll_publish'] : '0') . "', poll_edit='" . (int)(isset($admin_common_permission['poll_edit']) ? $admin_common_permission['poll_edit'] : '0') . "', poll_vote='" . (int)(isset($admin_common_permission['poll_vote']) ? $admin_common_permission['poll_vote'] : '0') . "', posting_approval='" . (int)(isset($admin_common_permission['posting_approval']) ? $admin_common_permission['posting_approval'] : '0') . "', posting_bbcode='" . (int)(isset($admin_common_permission['posting_bbcode']) ? $admin_common_permission['posting_bbcode'] : '0') . "', posting_bbcode_img='" . (int)(isset($admin_common_permission['posting_bbcode_img']) ? $admin_common_permission['posting_bbcode_img'] : '0') . "', posting_bbcode_flash='" . (int)(isset($admin_common_permission['posting_bbcode_flash']) ? $admin_common_permission['posting_bbcode_flash'] : '0') . "', posting_bbcode_url='" . (int)(isset($admin_common_permission['posting_bbcode_url']) ? $admin_common_permission['posting_bbcode_url'] : '0') . "', posting_smiles='" . (int)(isset($admin_common_permission['posting_smiles']) ? $admin_common_permission['posting_smiles'] : '0') . "' WHERE forum_id='" . (int)$forum_id . "' AND role_id='" . (int)$admin_common_permission['role_id'] . "' AND rank_id='" . (int)$admin_common_permission['rank_id'] . "'");
            }
        }

        //update user defined group common permission
        if(isset($data['user_defined_group_common_permission']) && $data['user_defined_group_common_permission']){
            foreach($data['user_defined_group_common_permission'] as $user_defined_group_common_permission){
                $this->db->query("UPDATE " . DB_PREFIX . "community_permission SET view_forum='" . (int)(isset($user_defined_group_common_permission['view_forum']) ? $user_defined_group_common_permission['view_forum'] : '0') . "', topic_view='" . (int)(isset($user_defined_group_common_permission['topic_view']) ? $user_defined_group_common_permission['topic_view'] : '0') . "', topic_publish='" . (int)(isset($user_defined_group_common_permission['topic_publish']) ? $user_defined_group_common_permission['topic_publish'] : '0') . "', topic_reply='" . (int)(isset($user_defined_group_common_permission['topic_reply']) ? $user_defined_group_common_permission['topic_reply'] : '0') . "', post_delete='" . (int)(isset($user_defined_group_common_permission['post_delete']) ? $user_defined_group_common_permission['post_delete'] : '0') . "', post_edit='" . (int)(isset($user_defined_group_common_permission['post_edit']) ? $user_defined_group_common_permission['post_edit'] : '0') . "', poll_publish='" . (int)(isset($user_defined_group_common_permission['poll_publish']) ? $user_defined_group_common_permission['poll_publish'] : '0') . "', poll_edit='" . (int)(isset($user_defined_group_common_permission['poll_edit']) ? $user_defined_group_common_permission['poll_edit'] : '0') . "', poll_vote='" . (int)(isset($user_defined_group_common_permission['poll_vote']) ? $user_defined_group_common_permission['poll_vote'] : '0') . "', posting_approval='" . (int)(isset($user_defined_group_common_permission['posting_approval']) ? $user_defined_group_common_permission['posting_approval'] : '0') . "', posting_bbcode='" . (int)(isset($user_defined_group_common_permission['posting_bbcode']) ? $user_defined_group_common_permission['posting_bbcode'] : '0') . "', posting_bbcode_img='" . (int)(isset($user_defined_group_common_permission['posting_bbcode_img']) ? $user_defined_group_common_permission['posting_bbcode_img'] : '0') . "', posting_bbcode_flash='" . (int)(isset($user_defined_group_common_permission['posting_bbcode_flash']) ? $user_defined_group_common_permission['posting_bbcode_flash'] : '0') . "', posting_bbcode_url='" . (int)(isset($user_defined_group_common_permission['posting_bbcode_url']) ? $user_defined_group_common_permission['posting_bbcode_url'] : '0') . "', posting_smiles='" . (int)(isset($user_defined_group_common_permission['posting_smiles']) ? $user_defined_group_common_permission['posting_smiles'] : '0') . "' WHERE forum_id='" . (int)$forum_id . "' AND role_id='" . (int)$user_defined_group_common_permission['role_id'] . "' AND rank_id='" . (int)$user_defined_group_common_permission['rank_id'] . "'");
            }
        }

        //update other group common permission
        if(isset($data['other_group_common_permission']) && $data['other_group_common_permission']){
            foreach($data['other_group_common_permission'] as $other_group_common_permission){
                $this->db->query("UPDATE " . DB_PREFIX . "community_permission SET view_forum='" . (int)(isset($other_group_common_permission['view_forum']) ? $other_group_common_permission['view_forum'] : '0') . "', topic_view='" . (int)(isset($other_group_common_permission['topic_view']) ? $other_group_common_permission['topic_view'] : '0') . "', topic_publish='" . (int)(isset($other_group_common_permission['topic_publish']) ? $other_group_common_permission['topic_publish'] : '0') . "', topic_reply='" . (int)(isset($other_group_common_permission['topic_reply']) ? $other_group_common_permission['topic_reply'] : '0') . "', post_delete='" . (int)(isset($other_group_common_permission['post_delete']) ? $other_group_common_permission['post_delete'] : '0') . "', post_edit='" . (int)(isset($other_group_common_permission['post_edit']) ? $other_group_common_permission['post_edit'] : '0') . "', poll_publish='" . (int)(isset($other_group_common_permission['poll_publish']) ? $other_group_common_permission['poll_publish'] : '0') . "', poll_edit='" . (int)(isset($other_group_common_permission['poll_edit']) ? $other_group_common_permission['poll_edit'] : '0') . "', poll_vote='" . (int)(isset($other_group_common_permission['poll_vote']) ? $other_group_common_permission['poll_vote'] : '0') . "', posting_approval='" . (int)(isset($other_group_common_permission['posting_approval']) ? $other_group_common_permission['posting_approval'] : '0') . "', posting_bbcode='" . (int)(isset($other_group_common_permission['posting_bbcode']) ? $other_group_common_permission['posting_bbcode'] : '0') . "', posting_bbcode_img='" . (int)(isset($other_group_common_permission['posting_bbcode_img']) ? $other_group_common_permission['posting_bbcode_img'] : '0') . "', posting_bbcode_flash='" . (int)(isset($other_group_common_permission['posting_bbcode_flash']) ? $other_group_common_permission['posting_bbcode_flash'] : '0') . "', posting_bbcode_url='" . (int)(isset($other_group_common_permission['posting_bbcode_url']) ? $other_group_common_permission['posting_bbcode_url'] : '0') . "', posting_smiles='" . (int)(isset($other_group_common_permission['posting_smiles']) ? $other_group_common_permission['posting_smiles'] : '0') . "' WHERE forum_id='" . (int)$forum_id . "' AND role_id='" . (int)$other_group_common_permission['role_id'] . "' AND rank_id='" . (int)$other_group_common_permission['rank_id'] . "'");
            }
        }
        
        //update register user rank common permission
        if(isset($data['member_common_permission']) && $data['member_common_permission']){
            /*foreach($data['member_common_permission'] as $member_common_permission){
                $this->db->query("UPDATE " . DB_PREFIX . "community_permission SET view_forum='" . (int)(isset($member_common_permission['view_forum']) ? $member_common_permission['view_forum'] : '0') . "', topic_view='" . (int)(isset($member_common_permission['topic_view']) ? $member_common_permission['topic_view'] : '0') . "', topic_publish='" . (int)(isset($member_common_permission['topic_publish']) ? $member_common_permission['topic_publish'] : '0')) . "', topic_reply='" . (int)(isset($member_common_permission['topic_reply']) ? $member_common_permission['topic_reply'] : '0' . "', post_delete='" . (int)(isset($member_common_permission['post_delete']) ? $member_common_permission['post_delete'] : '0') . "', post_edit='" . (int)(isset($member_common_permission['post_edit']) ? $member_common_permission['post_edit'] : '0') . "', poll_publish='" . (int)(isset($member_common_permission['poll_publish']) ? $member_common_permission['poll_publish'] : '0') . "', poll_edit='" . (int)(isset($member_common_permission['poll_edit']) ? $member_common_permission['poll_edit'] : '0') . "', poll_vote='" . (int)(isset($member_common_permission['poll_vote']) ? $member_common_permission['poll_vote'] : '0') . "', posting_approval='" . (int)(isset($member_common_permission['posting_approval']) ? $member_common_permission['posting_approval'] : '0') . "', posting_bbcode='" . (int)(isset($member_common_permission['posting_bbcode']) ? $member_common_permission['posting_bbcode'] : '0') . "', posting_bbcode_img='" . (int)(isset($member_common_permission['posting_bbcode_img']) ? $member_common_permission['posting_bbcode_img'] : '0') . "', posting_bbcode_flash='" . (int)(isset($member_common_permission['posting_bbcode_flash']) ? $member_common_permission['posting_bbcode_flash'] : '0') . "', posting_bbcode_url='" . (int)(isset($member_common_permission['posting_bbcode_url']) ? $member_common_permission['posting_bbcode_url'] : '0') . "', posting_smiles='" . (int)(isset($member_common_permission['posting_smiles']) ? $member_common_permission['posting_smiles'] : '') . "' WHERE forum_id='" . (int)$forum_id . "' AND role_id='" . (int)$member_common_permission['role_id'] . "' AND rank_id='" . (int)$member_common_permission['rank_id'] . "'");
            }*/

            foreach($data['member_common_permission'] as $member_common_permission){
                $this->db->query("UPDATE " . DB_PREFIX . "community_permission SET view_forum='" . (int)(isset($member_common_permission['view_forum']) ? $member_common_permission['view_forum'] : '0') . "', topic_view='" . (int)(isset($member_common_permission['topic_view']) ? $member_common_permission['topic_view'] : '0') . "', topic_publish='" . (int)(isset($member_common_permission['topic_publish']) ? $member_common_permission['topic_publish'] : '0') . "', topic_reply='" . (int)(isset($member_common_permission['topic_reply']) ? $member_common_permission['topic_reply'] : '0') . "', post_delete='" . (int)(isset($member_common_permission['post_delete']) ? $member_common_permission['post_delete'] : '0') . "', post_edit='" . (int)(isset($member_common_permission['post_edit']) ? $member_common_permission['post_edit'] : '0') . "', poll_publish='" . (int)(isset($member_common_permission['poll_publish']) ? $member_common_permission['poll_publish'] : '0') . "', poll_edit='" . (int)(isset($member_common_permission['poll_edit']) ? $member_common_permission['poll_edit'] : '0') . "', poll_vote='" . (int)(isset($member_common_permission['poll_vote']) ? $member_common_permission['poll_vote'] : '0') . "', posting_approval='" . (int)(isset($member_common_permission['posting_approval']) ? $member_common_permission['posting_approval'] : '0') . "', posting_bbcode='" . (int)(isset($member_common_permission['posting_bbcode']) ? $member_common_permission['posting_bbcode'] : '0') . "', posting_bbcode_img='" . (int)(isset($member_common_permission['posting_bbcode_img']) ? $member_common_permission['posting_bbcode_img'] : '0') . "', posting_bbcode_flash='" . (int)(isset($member_common_permission['posting_bbcode_flash']) ? $member_common_permission['posting_bbcode_flash'] : '0') . "', posting_bbcode_url='" . (int)(isset($member_common_permission['posting_bbcode_url']) ? $member_common_permission['posting_bbcode_url'] : '0') . "', posting_smiles='" . (int)(isset($member_common_permission['posting_smiles']) ? $member_common_permission['posting_smiles'] : '0') . "' WHERE forum_id='" . (int)$forum_id . "' AND role_id='" . (int)$member_common_permission['role_id'] . "' AND rank_id='" . (int)$member_common_permission['rank_id'] . "'");
            }
        }
	}
	
	public function deleteForum($forum_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "community_forum WHERE forum_id = '" . (int)$forum_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "community_forum_name_des_rule WHERE forum_id = '" . (int)$forum_id . "'");
        $this->db->query("DELETE FROM " . DB_PREFIX . "community_topic_category WHERE forum_id = '" . (int)$forum_id . "'");
        $this->db->query("DELETE FROM " . DB_PREFIX . "community_topic_category_title WHERE forum_id = '" . (int)$forum_id . "'");
        $this->db->query("DELETE FROM " . DB_PREFIX . "community_permission WHERE forum_id='" . (int)$forum_id . "'");
	}

    public function validateDelete($forum_id){
        $query = $this->db->query("SELECT forum_posts FROM " . DB_PREFIX . "community_forum WHERE forum_id='" . (int)$forum_id . "' LIMIT 0,1");

        $childForumCountQuery=$this->db->query("SELECT count(*) AS total FROM " . DB_PREFIX . "community_forum WHERE parent_id='" . (int)$forum_id . "'");

        $result=array();

        $result[]=$query->row['forum_posts'];
        $result[]=$childForumCountQuery->row['total'];

        return $result;
    }

    private function getAllUserRankIds(){
        $query=$this->db->query("SELECT rank_id FROM " . DB_PREFIX . "community_ranks");

        return $query->rows;
    }

    private function getAllUserDefinedUserGroups(){
        $query=$this->db->query("SELECT role_id FROM " . DB_PREFIX . "community_role WHERE role_id>7");

        return $query->rows;
    }

    public function getForumCategories() {
        $query = $this->db->query("SELECT f.forum_id, f.sort_order_id, n.forum_name, n.forum_desc, n.forum_rules FROM " . DB_PREFIX . "community_forum AS f LEFT JOIN " . DB_PREFIX . "community_forum_name_des_rule AS n ON (n.forum_id=f.forum_id AND n.language_id = '" . (int)$this->config->get('config_language_id') . "') WHERE f.forum_cat_id = '0' ORDER BY f.sort_order_id ASC");

        return $query->rows;
    }

    public function getForums($category_id) {
		$query = $this->db->query("SELECT f.forum_id, f.sort_order_id, n.forum_name, n.forum_desc, n.forum_rules FROM " . DB_PREFIX . "community_forum AS f LEFT JOIN " . DB_PREFIX . "community_forum_name_des_rule AS n ON (n.forum_id=f.forum_id AND n.language_id = '" . (int)$this->config->get('config_language_id') . "') WHERE f.forum_cat_id ='" . (int)$category_id . "' AND f.parent_id = '0' ORDER BY f.sort_order_id ASC");
		
		return $query->rows;
	}

    public function getSubForums($parent_id) {
        $query = $this->db->query("SELECT f.forum_id, f.sort_order_id, n.forum_name, n.forum_desc, n.forum_rules FROM " . DB_PREFIX . "community_forum AS f LEFT JOIN " . DB_PREFIX . "community_forum_name_des_rule AS n ON (n.forum_id=f.forum_id AND n.language_id = '" . (int)$this->config->get('config_language_id') . "') WHERE f.parent_id = '" . (int)$parent_id . "' ORDER BY f.sort_order_id ASC");

        return $query->rows;
    }

	public function getCategoryOrForumInfo($forum_id) {
        $data = array();

        $query = $this->db->query("SELECT n.forum_name, n.forum_desc, n.forum_rules, n.language_id, f.forum_id, f.forum_cat_id, f.parent_id, f.sort_order_id, f.topic_category_enabled, f.topic_category_displayed, f.forum_mod_ids, f.forum_enabled, f.forum_type FROM " . DB_PREFIX . "community_forum AS f LEFT JOIN " . DB_PREFIX . "community_forum_name_des_rule AS n ON (n.forum_id=f.forum_id) WHERE f.forum_id='" . (int)$forum_id . "'");

        foreach ($query->rows as $queryResult) {
            $data['forum_name'][$queryResult['language_id']] = $queryResult['forum_name'];
            $data['forum_desc'][$queryResult['language_id']] = $queryResult['forum_desc'];
            $data['forum_rules'][$queryResult['language_id']] = $queryResult['forum_rules'];
        }

        foreach($queryResult as $key => $result){
            if($key!='forum_name' && $key!='forum_desc' && $key!='forum_rules'){
                $data[$key]=$result;
            }
        }

		return $data;
	}

    public function getParentForums() {
        $query = $this->db->query("SELECT f.forum_id, n.forum_name FROM " . DB_PREFIX . "community_forum AS f LEFT JOIN " . DB_PREFIX . "community_forum_name_des_rule AS n ON (n.forum_id=f.forum_id AND n.language_id = '" . (int)$this->config->get('config_language_id') . "') WHERE f.forum_cat_id<>'0' ORDER BY f.sort_order_id ASC, n.forum_name ASC");

        return $query->rows;
    }

    public function getForumMods($forum_id){
        $query = $this->db->query("SELECT forum_mod_ids FROM " . DB_PREFIX . "community_forum WHERE forum_id='" . (int)$forum_id . "' LIMIT 0,1");

        $results=array();

        if(unserialize($query->row['forum_mod_ids'])){
            foreach(unserialize($query->row['forum_mod_ids']) as $forum_mod_id){
                $modQuery=$this->db->query("SELECT username FROM " . DB_PREFIX . "community_users WHERE user_id='" . (int)$forum_mod_id . "' LIMIT 0,1");
                $results[]=array(
                    'mod_id'      =>      $forum_mod_id,
                    'mod_name'    =>      $modQuery->row['username']
                );
            }
        }

        return $results;
    }

    public function getTopicTags($forum_id){
        $data=array();

        $query=$this->db->query("SELECT * FROM " . DB_PREFIX . "community_topic_category WHERE forum_id='" . (int)$forum_id . "' ORDER BY sort_order_id ASC");

        $i=0;

        foreach ($query->rows as $result) {
            $titleQuery=$this->db->query("SELECT * FROM " . DB_PREFIX . "community_topic_category_title WHERE topic_category_id='" . (int)$result['topic_category_id'] . "' ORDER BY language_id ASC");

            $data[$i]['topic_category_id']=$result['topic_category_id'];
            $data[$i]['sort_order_id']=$result['sort_order_id'];

            foreach($titleQuery->rows as $titleResult){
                $data[$i]['topic_category_title'][$titleResult['language_id']]=$titleResult['topic_category_title'];
            }

            $i++;
        }

        return $data;
    }

    public function getUsers($data){
        $query = $this->db->query("SELECT user_id, username FROM " . DB_PREFIX . "community_users WHERE LCASE(username) LIKE '" . $this->db->escape(utf8_strtolower($data['filter_name'])) . "%' LIMIT " . $data['start'] . "," . $data['limit']);

        return $query->rows;
    }

    public function addMod($getData,$postData){
        $userQuery = $this->db->query("SELECT user_id FROM " . DB_PREFIX . "community_users WHERE LCASE(username) = '" . $this->db->escape(utf8_strtolower($postData['forum_mod_name'])) . "' LIMIT 0,1");

        if(!$userQuery->row){
            return FALSE;
        }else{
            $modQuery = $this->db->query("SELECT forum_mod_ids FROM " . DB_PREFIX . "community_forum WHERE forum_id = '" . (int)$getData['forum_id'] . "' LIMIT 0,1");

            $mod_ids = array();

            if($modQuery->row && $modQuery->row['forum_mod_ids']){
                $mod_ids = unserialize($modQuery->row['forum_mod_ids']);
            }

            if(!in_array($userQuery->row['user_id'],$mod_ids)){
                array_push($mod_ids,$userQuery->row['user_id']);
            }

            $this->db->query("UPDATE " . DB_PREFIX . "community_forum AS f LEFT JOIN " . DB_PREFIX . "community_users AS u ON (u.user_id='" . (int)$userQuery->row['user_id'] . "') SET f.forum_mod_ids = '" . serialize($mod_ids) . "', u.user_type = '3' WHERE f.forum_id = '" . (int)$getData['forum_id'] . "'");

            return TRUE;
        }
    }

    public function removeMod($getData,$postData){
        $modQuery = $this->db->query("SELECT forum_mod_ids FROM " . DB_PREFIX . "community_forum WHERE forum_id = '" . (int)$getData['forum_id'] . "' LIMIT 0,1");

        $mod_ids = unserialize($modQuery->row['forum_mod_ids']);

        foreach($mod_ids as $key => $mod_id){
            if($mod_id==$postData['remove_mod']){
                unset($mod_ids[$key]);
            }
        }

        $this->db->query("UPDATE " . DB_PREFIX . "community_forum AS f LEFT JOIN " . DB_PREFIX . "community_users AS u ON (u.user_id='" . (int)$postData['remove_mod'] . "') SET f.forum_mod_ids = '" . serialize($mod_ids) . "', u.user_type = '5' WHERE f.forum_id = '" . (int)$getData['forum_id'] . "'");
    }

    public function getAdministrativePermission($forum_id){
        $query=$this->db->query("SELECT p.role_id, p.rank_id, p.topic_announcement, p.topic_global_announcement, p.topic_featured, p.topic_sticky, p.topic_move, p.topic_lock, p.topic_solve, p.topic_unsolve, p.edit_lock, p.post_approve, p.post_recycle_bin_clear, p.post_members_edit, p.post_members_delete, rt.role_title FROM " . DB_PREFIX . "community_permission AS p LEFT JOIN " . DB_PREFIX . "community_role_title AS rt ON (rt.role_id=p.role_id AND rt.language_id='" . (int)$this->config->get('config_language_id') . "') WHERE p.role_id<='3' AND p.forum_id='" . (int)$forum_id . "' ORDER BY p.role_id ASC");

        return $query->rows;
    }

    public function getAdminCommonPermission($forum_id){
        $query=$this->db->query("SELECT p.role_id, p.rank_id, p.view_forum, p.topic_view, p.topic_publish, p.topic_reply, p.post_delete, p.post_edit, p.poll_publish, p.poll_edit, p.poll_vote, p.posting_approval, p.posting_bbcode, p.posting_bbcode_img, p.posting_bbcode_flash, p.posting_bbcode_url, p.posting_smiles, rt.role_title FROM " . DB_PREFIX . "community_permission AS p LEFT JOIN " . DB_PREFIX . "community_role_title AS rt ON (rt.role_id=p.role_id AND rt.language_id='" . (int)$this->config->get('config_language_id') . "') WHERE p.forum_id='" . (int)$forum_id . "' AND p.role_id<='3' ORDER BY p.role_id ASC");

        return $query->rows;
    }

    public function getOtherGroupCommonPermission($forum_id){
        $query=$this->db->query("SELECT p.role_id, p.rank_id, p.view_forum, p.topic_view, p.topic_publish, p.topic_reply, p.post_delete, p.post_edit, p.poll_publish, p.poll_edit, p.poll_vote, p.posting_approval, p.posting_bbcode, p.posting_bbcode_img, p.posting_bbcode_flash, p.posting_bbcode_url, p.posting_smiles, rt.role_title FROM " . DB_PREFIX . "community_permission AS p LEFT JOIN " . DB_PREFIX . "community_role_title AS rt ON (rt.role_id=p.role_id AND rt.language_id='" . (int)$this->config->get('config_language_id') . "') WHERE p.forum_id='" . (int)$forum_id . "' AND p.role_id>'3' AND p.role_id<='7' AND p.role_id<>5 ORDER BY p.role_id ASC");

        return $query->rows;
    }

    public function getUserDefinedGroupCommonPermission($forum_id){
        $query=$this->db->query("SELECT p.role_id, p.rank_id, p.view_forum, p.topic_view, p.topic_publish, p.topic_reply, p.post_delete, p.post_edit, p.poll_publish, p.poll_edit, p.poll_vote, p.posting_approval, p.posting_bbcode, p.posting_bbcode_img, p.posting_bbcode_flash, p.posting_bbcode_url, p.posting_smiles, rt.role_title FROM " . DB_PREFIX . "community_permission AS p LEFT JOIN " . DB_PREFIX . "community_role_title AS rt ON (rt.role_id=p.role_id AND rt.language_id='" . (int)$this->config->get('config_language_id') . "') WHERE p.forum_id='" . (int)$forum_id . "' AND p.role_id>'7' ORDER BY p.role_id ASC");

        return $query->rows;
    }

    public function getMemberCommonPermission($forum_id){
        $query=$this->db->query("SELECT p.role_id, p.rank_id, p.view_forum, p.topic_view, p.topic_publish, p.topic_reply, p.post_delete, p.post_edit, p.poll_publish, p.poll_edit, p.poll_vote, p.posting_approval, p.posting_bbcode, p.posting_bbcode_img, p.posting_bbcode_flash, p.posting_bbcode_url, p.posting_smiles, rt.rank_title FROM " . DB_PREFIX . "community_permission AS p LEFT JOIN " . DB_PREFIX . "community_rank_title AS rt ON (rt.rank_id=p.rank_id AND rt.language_id='" . (int)$this->config->get('config_language_id') . "') WHERE p.forum_id='" . (int)$forum_id . "' AND p.role_id='5' ORDER BY p.rank_id ASC");

        return $query->rows;
    }
}
?>